import { formatTime } from "./date";

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

function sleep(second) {
  let sec = second || 1;
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve()
    }, sec * 1000);
  })
}

const isEmpty = t => {
  if (t === undefined || t === null || t === '') {
    return true
  } else {
    return false
  }
}

const isEmptyObj = t => {
  if (isEmpty(t)) {
    return true
  } else if (Array.isArray(t) && t.length == 0) {
    return true
  } else if (
    Object.prototype.toString.call(t) == "[object Object]" &&
    Object.keys(t).length == 0) {
    return true
  }
  return false
}

// 转换为数字，第二位是精度
function toNumber(number, decimalPlaces) {
  var multiplier = Math.pow(10, decimalPlaces);
  var result = Math.round(number * multiplier) / multiplier;
  return result;
}

// 返回时间的年月
function yearAndMonth(t) {
  if (t === undefined || t === null || t === '') {
    return []
  } else {
    const date = new Date(t);
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    return [year, month];
  }
}

function calculateRetirement(
  birthYear,
  birthMonth,
  gender,
  originalRetirementAge
) {
  // 将出生年月转换为Date对象
  const birthDate = new Date(birthYear, birthMonth - 1, 1);

  // 计算原始退休日期
  const originalRetirementDate = new Date(birthDate);
  originalRetirementDate.setFullYear(
    originalRetirementDate.getFullYear() + originalRetirementAge
  );

  // 政策开始实施的日期
  const policyStartDate = new Date(2025, 0, 1);

  // 如果原始退休日期在2025年1月1日之前，则不受新政策影响
  if (originalRetirementDate < policyStartDate) {
    return formatResult(originalRetirementDate, 0, birthDate);
  }

  let targetAge, monthsPerDelay;

  if (gender === "male") {
    targetAge = 63;
    monthsPerDelay = 4;
  } else if (gender === "female" && originalRetirementAge === 55) {
    targetAge = 58;
    monthsPerDelay = 4;
  } else if (gender === "female" && originalRetirementAge === 50) {
    targetAge = 55;
    monthsPerDelay = 2;
  } else {
    throw new Error("Invalid gender or original retirement age");
  }

  // 计算从政策开始日期到原始退休日期的月数
  const monthsFromPolicyStart =
    (originalRetirementDate.getFullYear() - policyStartDate.getFullYear()) *
    12 +
    (originalRetirementDate.getMonth() - policyStartDate.getMonth());

  // 计算应延迟的月数，加1是因为第一个延迟周期也要计算在内
  const shouldDelayMonths =
    Math.floor(monthsFromPolicyStart / monthsPerDelay) + 1;

  // 确保不超过最大延迟月数
  const maxDelayMonths = (targetAge - originalRetirementAge) * 12;
  const actualDelayedMonths = Math.min(shouldDelayMonths, maxDelayMonths);

  // 计算新的退休日期
  const newRetirementDate = new Date(originalRetirementDate);
  newRetirementDate.setMonth(
    newRetirementDate.getMonth() + actualDelayedMonths
  );

  return formatResult(newRetirementDate, actualDelayedMonths, birthDate);
}


function formatResult(retirementDate, delayedMonths, birthDate) {
  const retirementYear = retirementDate.getFullYear();
  const retirementMonth = retirementDate.getMonth() + 1;

  // 计算退休年龄
  const retirementAgeYears = retirementYear - birthDate.getFullYear();
  const retirementAgeMonths = retirementMonth - (birthDate.getMonth() + 1);
  let retirementAge = retirementAgeYears;
  let retirementAgeExtraMonths = retirementAgeMonths;
  if (retirementAgeMonths < 0) {
    retirementAge--;
    retirementAgeExtraMonths += 12;
  }

  // 计算距离退休还剩多少年月
  const now = new Date();
  const monthsToRetirement =
    (retirementYear - now.getFullYear()) * 12 +
    (retirementMonth - (now.getMonth() + 1));
  const yearsToRetirement = Math.floor(monthsToRetirement / 12);
  const extraMonthsToRetirement = monthsToRetirement % 12;

  let timeToRetirement = `${yearsToRetirement > 0 ? yearsToRetirement + "年" : ""
    }${extraMonthsToRetirement > 0 ? extraMonthsToRetirement + "个月" : ""}`;
  if (yearsToRetirement < 0) {
    timeToRetirement = "已退休";
  }
  return {
    delayedMonths: delayedMonths,
    retirementDate: `${retirementYear}年${retirementMonth}月`,
    retirementAge: `${retirementAge}岁${retirementAgeExtraMonths > 0
      ? "+" + retirementAgeExtraMonths + "个月"
      : ""
      }`,
    timeToRetirement,
  };
}

export {
  formatTime,
  sleep,
  isEmpty,
  isEmptyObj,
  toNumber,
  yearAndMonth,
  calculateRetirement,
  formatResult,

}
